/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Izmena.java
 *
 * Created on Nov 25, 2011, 9:00:00 AM
 */
package tok.predmeta;

import java.awt.Component;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author root
 */
public class Izmena extends javax.swing.JFrame {

    private Component frame;

    /** Creates new form Izmena */
    public Izmena() {
        initComponents();
        jTextField7.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                warn1();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                warn1();
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                warn1();
            }

            public void warn1() {
                if (jTextField7.getText().equals("01")  ) {
                    if (jTextField1.getText().equals("") || jTextField2.getText().equals("") || jTextField3.getText().equals("") || jTextArea1.getText().equals("")
                            || jTextField4.getText().equals("") || jTextField5.getText().equals("") || jTextField6.getText().equals("")) {
                        JOptionPane.showMessageDialog(frame,
                                "Молимо попуните сва поља !",
                                "Први основни суд у Београду",
                                JOptionPane.WARNING_MESSAGE);

                    } else {

                        String serverName = "192.168.88.10";
                        String mydatabase = "tok_predmeta";
                        String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?useUnicode=true&characterEncoding=utf8"; //?useUnicode=true&amp;characterEncoding=Cp1251"; // a JDBC url
                        String username = "tokpred";
                        String password = "Lx3sAQdnYJFTxBhs";
                        String DATE_FORMAT = "dd/MM/yyyy";
                        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
                        Calendar c1 = Calendar.getInstance(); // today
                        String Datum = sdf.format(c1.getTime()).toString();
                        try {
                            java.sql.Connection connection = DriverManager.getConnection(url, username, password);
                            java.sql.PreparedStatement i = connection.prepareStatement("INSERT INTO predmeti (sifra, ime,prezime,adresa,jmbg,tip,datum,korisnik,korisnicka_sifra) VALUES "
                                    + "(?,?,?,?,?,?,?,?,?)");


                            String stanje = jTextField4.getText();


                            i.setString(1, jTextField4.getText());
                            i.setString(2, jTextField1.getText());
                            i.setString(3, jTextField2.getText());
                            i.setString(4, jTextArea1.getText());
                            i.setString(5, jTextField5.getText());
                            i.setObject(6, jComboBox1.getSelectedItem());
                            i.setString(7, Datum);
                            i.setString(8, Login.user);
                            i.setObject(9, jTextField6.getText());

                            i.executeUpdate();

                            java.sql.PreparedStatement e = connection.prepareStatement("INSERT INTO state (sifra,korisnicka_sifra,stanje_predmeta,date,korisnik) VALUES "
                                    + "(?,?,?,?,?)");

                            e.setString(1, jTextField4.getText());
                            e.setString(2, jTextField6.getText());
                            e.setString(3, jTextField3.getText());
                            e.setString(4, Datum);
                            e.setString(5, Login.user);
                            e.executeUpdate();
                            dispose();
                            Frame1 P1 = new Frame1();
                            P1.setVisible(true);

                            i.close();
                        } catch (SQLException ex) {
                            Logger.getLogger(Pretraga.class.getName()).log(Level.SEVERE, null, ex);
                            JOptionPane.showMessageDialog(frame,
                                    "Грешка приликом уписа у базу података. Предмет са таквим бројем или шифра корисника већ постоји !",
                                    "Први основни суд у Београду",
                                    JOptionPane.WARNING_MESSAGE);
                        }
                    }
                }else if(jTextField7.getText().equals("02")){
                    Frame1 F1 = new Frame1();
                    dispose();
                    F1.setVisible(true);
                }else{
                    
                }
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Први основни суд у Београду - Ток обраде захтева");

        jLabel1.setText("Име");

        jLabel2.setText("Презиме");

        jLabel3.setText("Адреса");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Притужба", "Захтев за информацију од јавног значаја", "Захтев за изузеће", "Представка" }));

        jLabel4.setText("Тип предмета");

        jLabel5.setText("Локација предмета");

        jLabel6.setText("Шифра предмета");

        jButton1.setText("Сними");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Одустани");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel7.setText("ЈМБГ");

        jLabel8.setText("Корисничка шифра");

        jLabel9.setText("Сними / Одустани");

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel10.setText("ДОДАВАЊЕ НОВОГ ПРЕДМЕТА");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addGap(99, 99, 99)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1)
                    .addComponent(jTextField1)
                    .addComponent(jTextField3)
                    .addComponent(jTextField6))
                .addGap(69, 69, 69)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel7)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(43, 43, 43)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                                    .addComponent(jComboBox1, 0, 0, Short.MAX_VALUE)
                                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                                    .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)))))
                    .addComponent(jLabel9)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 205, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addGap(94, 94, 94))
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel10)
                .addContainerGap(543, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addContainerGap(718, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addContainerGap(719, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jLabel10)
                .addGap(83, 83, 83)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel7)
                                .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))))
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addGap(58, 58, 58))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Frame1 F1 = new Frame1();
        this.dispose();
        F1.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (jTextField1.getText().equals("") || jTextField2.getText().equals("") || jTextField3.getText().equals("") || jTextArea1.getText().equals("")
                || jTextField4.getText().equals("") || jTextField5.getText().equals("") || jTextField6.getText().equals("")) {
            JOptionPane.showMessageDialog(frame,
                    "Молимо попуните сва поља !",
                    "Први основни суд у Београду",
                    JOptionPane.WARNING_MESSAGE);

        } else {

            String serverName = "192.168.88.10";
            String mydatabase = "tok_predmeta";
            String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?useUnicode=true&characterEncoding=utf8"; //?useUnicode=true&amp;characterEncoding=Cp1251"; // a JDBC url
            String username = "tokpred";
            String password = "";//y43HzEtu2u3YLjLQ
            String DATE_FORMAT = "dd/MM/yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            Calendar c1 = Calendar.getInstance(); // today
            String Datum = sdf.format(c1.getTime()).toString();
            try {
                java.sql.Connection connection = DriverManager.getConnection(url, username, password);
                java.sql.PreparedStatement i = connection.prepareStatement("INSERT INTO predmeti (sifra, ime,prezime,adresa,jmbg,tip,datum,korisnik,korisnicka_sifra) VALUES "
                        + "(?,?,?,?,?,?,?,?,?)");


                String stanje = jTextField4.getText();


                i.setString(1, jTextField4.getText());
                i.setString(2, jTextField1.getText());
                i.setString(3, jTextField2.getText());
                i.setString(4, jTextArea1.getText());
                i.setString(5, jTextField5.getText());
                i.setObject(6, jComboBox1.getSelectedItem());
                i.setString(7, Datum);
                i.setString(8, Login.user);
                i.setObject(9, jTextField6.getText());

                i.executeUpdate();

                java.sql.PreparedStatement e = connection.prepareStatement("INSERT INTO state (sifra,korisnicka_sifra,stanje_predmeta,date,korisnik) VALUES "
                        + "(?,?,?,?,?)");

                e.setString(1, jTextField4.getText());
                e.setString(2, jTextField6.getText());
                e.setString(3, jTextField3.getText());
                e.setString(4, Datum);
                e.setString(5, Login.user);
                e.executeUpdate();
                this.dispose();
                Frame1 P1 = new Frame1();
                P1.setVisible(true);

                i.close();
            } catch (SQLException ex) {
                Logger.getLogger(Pretraga.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(frame,
                        "Грешка приликом уписа у базу података. Предмет са таквим бројем или шифра корисника већ постоји !",
                        "Први основни суд у Београду",
                        JOptionPane.WARNING_MESSAGE);
            }
        }


    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Izmena.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Izmena.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Izmena.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Izmena.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Izmena().setVisible(true);
            }
        });



    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    // End of variables declaration//GEN-END:variables
}
